package com.xmall.member.controller;

import com.xmall.aspect.OperateRecord;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.member.define.dto.CartDto;
import com.xmall.member.service.ICartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/service/member/cart")
@RestController
public class CartController {

    @Resource
    private ICartService cartService;

    /**
     * 列表
     */
    @RequestMapping("/list/{memberId}")
    public Result<List<CartDto>> list(@PathVariable("memberId") Long memberId) {
        return Result.success(cartService.list(memberId));
    }

    /**
     * 添加购物车
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @PostMapping("/addCart")
    public Result addCart(@RequestParam("memberId") Long memberId,
                      @RequestParam("skuId") Long skuId,
                      @RequestParam("quantity") Integer quantity) {
        cartService.addCart(memberId, skuId, quantity);
        return Result.success();
    }

    /**
     * 更新购物车
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PostMapping("/updateCart")
    public Result updateCart(@RequestParam("id") Long id, @RequestParam("quantity") Integer quantity) {
        cartService.updateCart(id, quantity);
        return Result.success();
    }

    /**
     * 删除购物车
     */
    @OperateRecord(type = OperateTypeEnum.DELETE)
    @PostMapping("/deleteCart")
    public Result deleteCart(@RequestBody List<Long> cartIds) {
        cartService.deleteCart(cartIds);
        return Result.success();
    }
}
